package com.xff.Controller;

import com.xff.bean.AdminBean;
import com.xff.bean.PositionBean;
import com.xff.bean.SeatBean;
import com.xff.bean.SubscribeBean;
import com.xff.dao.AdminDao;
import com.xff.dao.SeatDao;
import com.xff.dao.SubscribeDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class SeatController {

    /*
    * 修改一批座位为不可坐
    * */
    @ResponseBody
    @PostMapping("/SeatServlet")
    public HashMap<String,int[]> SeatServlet(HttpServletRequest request, HttpSession session){
            String row=request.getParameter("row");
            String column=request.getParameter("column");
            String rowlist[] =row.split("-");
            String columnlist[] =column.split("-");
            new SeatDao().updateSeatStatus(1,rowlist,columnlist);

           HashMap<String,int[]> status=new HashMap<>();
           status.put("status",new SeatDao().SelectStatus(1));
           return status;
    }


    /*
    * 修改全部座位状态
    * */
    @PostMapping("/AllSeatServlet")
    public void AllSeatServlet(HttpServletRequest request, HttpSession session){
        String tip1=request.getParameter("tip");
        int tip=Integer.parseInt(tip1);
        new SeatDao().updateAllSeatStatus(1,tip);
    }

    /*
    * 签退所有座位
    * */
    @PostMapping("/AllSignOutServlet")
    public void AllSignOutServlet(HttpServletRequest request, HttpSession session){

        SeatDao seatDao=new SeatDao();
        //将所有的读者选座签退
        seatDao.updateAllPositionStatus(0);
        //将所有有人的座位设置成可选
        seatDao.updateSeatChooseable();

    }

    @ResponseBody
    @PostMapping("/SelectSeatServlet")
    public HashMap<String,int[]> SelectSeatServlet(HttpServletRequest request, HttpSession session){

        HashMap<String,int[]> status=new HashMap<>();
        status.put("status",new SeatDao().SelectStatus(1));
        return status;
    }

    @ResponseBody
    @GetMapping("/SelectSoldSeatServlet")
    public List<SeatBean> SelectSoldSeatServlet(){
        return new SeatDao().SelectStatus(1,1,2);
    }

    @ResponseBody
    @GetMapping("/ChooseSeat")
    public String ChooseSeat(HttpServletRequest request){
        String username=request.getParameter("username");
        int seatrow=Integer.parseInt(request.getParameter("seatrow"));
        int seatcolumn=Integer.parseInt(request.getParameter("seatcolumn"));
        SeatDao seatDao=new SeatDao();
        seatDao.updateSeatStatus(1,seatrow,seatcolumn,2);

        AdminBean adminBean=new AdminDao().get_AidInfo3(username);
        SeatBean seatBean=seatDao.SelectSeat(1,seatrow,seatcolumn);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=format.format(new Date());
        seatDao.addPosition(new PositionBean(seatBean.getSeatid(),adminBean.getAid(),adminBean.getUsername(),adminBean.getName(),date,"",1));
        return "ok";
    }

    @ResponseBody
    @GetMapping("/SelectPositionStatusServlet")
    public String SelectPositionStatusServlet(HttpServletRequest request){
        String username =request.getParameter("username");
        PositionBean positionBean=new SeatDao().SelectMyPosition(username,1,2);
        if(positionBean.getStatus()==0){
            return "no";
        }
        else
            return positionBean.getStatus()+"";
    }

    @ResponseBody
    @GetMapping("/SelectMyPositionServlet")
    public PositionBean SelectMyPositionServlet(HttpServletRequest request){
        String username =request.getParameter("username");
        SeatDao seatDao = new SeatDao();
        PositionBean positionBean=seatDao.SelectMyPosition(username,1,2);
        SeatBean seatBean=seatDao.SelectSeat(positionBean.getSeatid());
        positionBean.setSeatrow(seatBean.getSeatrow());
        positionBean.setSeatcolumn(seatBean.getSeatcolumn());
        System.out.println(positionBean.getStatus()+"---"+positionBean.getSeatrow()+username);
        return positionBean;
    }

    /*
    * 取消预约
    * */
    @ResponseBody
    @GetMapping("/DeleteMyPosition")
    public String DeleteMyPosition(HttpServletRequest request){
        int positionid =Integer.parseInt(request.getParameter("positionid"));
        SeatDao seatDao = new SeatDao();
        //删除记录
        int seatid=seatDao.SelectSeatid(positionid);
        seatDao.deletePosition(positionid);
        //座位可选
        seatDao.updateOneSeatStatus(seatid);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/SignIn")
    public String SignIn(HttpServletRequest request){
        int positionid =Integer.parseInt(request.getParameter("positionid"));
        SeatDao seatDao = new SeatDao();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        seatDao.updatePositionStatus(positionid,format.format(new Date()),1);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/SignOut")
    public String SignOut(HttpServletRequest request){
        int positionid =Integer.parseInt(request.getParameter("positionid"));
        SeatDao seatDao = new SeatDao();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //修改状态
        seatDao.updatePositionStatus(positionid,format.format(new Date()),2);
        int seatid=seatDao.SelectSeatid(positionid);
        //座位可选
        seatDao.updateOneSeatStatus(seatid);

        return "ok";
    }
}
